package com.ymatou.apigateway.testcase;

import java.util.ArrayList;
import java.util.List;







import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;







import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqAgentPriceDto;
import com.ymatou.apigateway.parameter.SqCatalogDto;
import com.ymatou.apigateway.parameter.SqCatalogPropDto;
import com.ymatou.apigateway.parameter.SqProductSkuUpdateBean;
import com.ymatou.apigateway.parameter.SqSubCodeDto;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqProductSkuUpdateCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.CatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.CatalogSubCodeDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductListDto;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品sku
*FreeMaker Template自动生成代码
*com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductSku
*/
@TestSuite
public class Ts_SqProductSkuUpdate{
	
	private static OpenapiReq openapiReq;
	private static int RootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	private static long multiCDFCodeGoodsOwnerId = ShenqiUserSet.multiCDFCodeGoodsOwnerId;
	
	private static SqProductSkuUpdateBean sqproductskuupdateBean;
	private static SqProductSkuUpdateCall sqproductskuupdateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品sku");
	}

	@Before
	public void caseUp() {
		sqproductskuupdateBean=new SqProductSkuUpdateBean();
		sqproductskuupdateCall=new SqProductSkuUpdateCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.product.sku.update");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqproductskuupdateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SqProductSkuUpdate_001() {
		Logger.start(true,"更新商品sku");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
					"sellername", false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			sqproductskuupdateBean.setProduct_id(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
				b.setCostPrice(12.32);
				b.setBarcode("barcode"+TestDataManager.getRandomNum(5));
			});
			
			List<SqCatalogDto> catalog_list = new ArrayList<SqCatalogDto>();
			
			catalog_list = initCatalogList(catalog_list, catalogList);
			
			sqproductskuupdateBean.setCatalog_list(catalog_list);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductskuupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.shop2cn.com/apigateway/v1?app_id=%s&method=sq.product.sku.update",
							app_id);
			sqproductskuupdateCall.setUrl(url);
			//openapi设置完成
			
			sqproductskuupdateCall.setData(openapiReq);
			sqproductskuupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductskuupdateCall.getString("code"),
					"验证Code");
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			List<YmtCatalogsI> Catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			Logger.debug(FormatUtil.beanToGSONString(Catalogs));
			Logger.verifyEquals(Catalogs.get(0).getBarcode(), catalogList.get(0).getBarcode(), "验证条码");
			Logger.verifyEquals(Catalogs.get(1).getBarcode(), catalogList.get(1).getBarcode(), "验证条码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//在agentproduct做
	public void Tc_SqProductSkuUpdate_002() {
		Logger.start(true,"更新商品sku-批量商品");
		try {
			Integer mchId = GodmchManageCallService.CreateGodMch();
			Integer agenttype=GodmchManageCallService.getAgentType(mchId, 1);
			Integer agent_mchId = GodmchManageCallService.getAgentMchId(mchId, agenttype);
			createDepotBean bean=GodmchManageCallService.createDepot(mchId);
	        List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "", false);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			setConditionMarkupConfigBean setConditionMarkupConfigBean = GodmchManageCallService.initDefSetConditionMarkupConfig(agent_mchId, mchId, 1, addAgentProductBean, 1);
			GodmchManageCallService.SetConditionMarkupConfigCall(setConditionMarkupConfigBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			
			  List<ProductListDto> getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  int redo=0;
			  while(getProductLists2.size()<1&&redo<3) {
				  Thread.sleep(5000);
				  redo++;
				  getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			  }
			ProductListDto agentProd = getProductLists2.get(0);
			//商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(),1,"已经被打上自动加价的标");
			
			sqproductskuupdateBean.setProduct_id(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			catalogList.forEach(b->{
				b.setStock(10000);
				b.setDirectPrice(123.1);
				b.setVirtualStock(200);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
				b.setCostPrice(45.2);
				b.setBarcode("barcode"+TestDataManager.getRandomNum(5));
			});
			
			List<SqCatalogDto> catalog_list=new ArrayList<SqCatalogDto>();
			catalog_list=initCatalogList(catalog_list,catalogList);
			sqproductskuupdateBean.setCatalog_list(catalog_list);
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductskuupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.shop2cn.com/apigateway/v1?app_id=%s&method=sq.product.sku.update",
							app_id);
			sqproductskuupdateCall.setUrl(url);
			//openapi设置完成
			
			sqproductskuupdateCall.setData(openapiReq);
			sqproductskuupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductskuupdateCall.getString("code"),
					"验证Code");
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			List<YmtCatalogsI> Catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			Logger.debug(FormatUtil.beanToGSONString(Catalogs));
			Logger.verifyEquals(Catalogs.get(0).getBarcode(), catalogList.get(0).getBarcode(), "验证条码");
			Logger.verifyEquals(Catalogs.get(1).getBarcode(), catalogList.get(1).getBarcode(), "验证条码");
			
			getProductLists2 = AddAgentProductCallService.GetProductLists(agent_mchId);
			agentProd = getProductLists2.get(0);
			//商品打标
			Logger.verifyEquals(agentProd.getAutoMarkupStatus(),1,"已经被打上自动加价的标");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SqProductSkuUpdate_003() {
		Logger.start(true,"更新一品多码商品sku");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService
					.initAddMultiCodeAgentProduct((int) multiCDFCodeGoodsOwnerId,
							"sellername", false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			System.out.println(addAgentProductBean.getCatalogList().get(0).getStock());
			Thread.sleep(2000);
			sqproductskuupdateBean.setProduct_id(productId);
			List<CatalogDto> catalogList = addAgentProductBean.getCatalogList();
			
			catalogList.forEach(b->{
				b.setStock(1000);
				b.setDirectPrice(123.1);
				b.setWeight(20);
				b.setPic("http://1333");
				b.setLimitNum(123);
				b.setMarketAmount(123.4);
				b.setMarketPrice(321.5);
				b.setCostPrice(12.32);
				
				List<CatalogSubCodeDto> subCodeList = new ArrayList<>();

				CatalogSubCodeDto sqSubCodeDto1 = new CatalogSubCodeDto();

				sqSubCodeDto1.setCode(TestDataManager.getRandomUUID()
						.toString());
				sqSubCodeDto1.setAttr("subCodettr"
						+ TestDataManager.randomStr(5));
				sqSubCodeDto1.setEffectivePeriod("2030-04-09");
				sqSubCodeDto1.setStock(100);

				CatalogSubCodeDto sqSubCodeDto2 = new CatalogSubCodeDto();

				sqSubCodeDto2.setCode(TestDataManager.getRandomUUID()
						.toString());
				sqSubCodeDto2.setAttr("subCodettr"
						+ TestDataManager.randomStr(5));
				sqSubCodeDto2.setEffectivePeriod("2029-04-09");
				sqSubCodeDto2.setStock(100);

				subCodeList.add(sqSubCodeDto1);
				subCodeList.add(sqSubCodeDto2);

				b.setSubCodeList(subCodeList);
			});
			
			List<SqCatalogDto> catalog_list = new ArrayList<SqCatalogDto>();
			catalog_list = initCatalogList(catalog_list, catalogList);
			sqproductskuupdateBean.setCatalog_list(catalog_list);
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId(multiCDFCodeGoodsOwnerId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductskuupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.shop2cn.com/apigateway/v1?app_id=%s&method=sq.product.sku.update",
							app_id);
			sqproductskuupdateCall.setUrl(url);
			//openapi设置完成
			
			sqproductskuupdateCall.setData(openapiReq);
			sqproductskuupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductskuupdateCall.getString("code"),
					"验证Code");
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			List<YmtCatalogsI> Catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			Logger.debug(FormatUtil.beanToGSONString(Catalogs));
			Logger.verifyEquals(Catalogs.get(0).getBarcode(), catalogList.get(0).getBarcode(), "验证条码");
			Logger.verifyEquals(Catalogs.get(1).getBarcode(), catalogList.get(1).getBarcode(), "验证条码");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private List<SqCatalogDto> initCatalogList(List<SqCatalogDto> catalog_list,
			List<CatalogDto> catalogList) {
		catalogList
				.forEach(c1 -> {
					SqCatalogDto c2 = new SqCatalogDto();

					List<SqAgentPriceDto> sqAgentPriceDtoList = new ArrayList<>();

					c1.getAgentPriceList()
							.forEach(
									agentPriceDto -> {

										SqAgentPriceDto sqAgentPriceDto = new SqAgentPriceDto();

										sqAgentPriceDto
												.setAgent_model(agentPriceDto
														.getAgentModel());
										sqAgentPriceDto
												.setAgent_price(agentPriceDto
														.getAgentPrice());
										sqAgentPriceDto
												.setAgent_type_id(agentPriceDto
														.getAgentTypeId());
										sqAgentPriceDto
												.setAgent_type_name(agentPriceDto
														.getAgentTypeName());

										sqAgentPriceDtoList
												.add(sqAgentPriceDto);
									});

					c2.setAgent_price_list(sqAgentPriceDtoList);

					c2.setDirect_price(c1.getDirectPrice());
					c2.setLimit_num(c1.getLimitNum());
					c2.setMarket_price(c1.getMarketPrice());
					c2.setOuter_sku_id(c1.getSku() + "new");
					c2.setPic(c1.getPic());
					c2.setBarcode(c1.getBarcode());

					List<SqCatalogPropDto> sqCatalogPropDtoList = new ArrayList<>();

					c1.getPropertyList()
							.forEach(
									propertyDto -> {

										SqCatalogPropDto sqCatalogPropDto = new SqCatalogPropDto();

										sqCatalogPropDto.setName(propertyDto
												.getName());
										sqCatalogPropDto.setPic(propertyDto
												.getPic());
										sqCatalogPropDto
												.setSub_type(propertyDto
														.getSubType());
										sqCatalogPropDto.setValue(propertyDto
												.getValue());

										sqCatalogPropDtoList
												.add(sqCatalogPropDto);
									});

					c2.setProperty_list(sqCatalogPropDtoList);

					c2.setStock(c1.getStock());
					c2.setWeight(c1.getWeight());
					c2.setCost_price(c1.getCostPrice());

					if (null != c1.getSubCodeList()) {

						List<SqSubCodeDto> sqSubCodeDtoList = new ArrayList<>();

						c1.getSubCodeList()
								.forEach(
										subCodeDto -> {

											SqSubCodeDto sqSubCodeDto = new SqSubCodeDto();

											sqSubCodeDto.setAttr(subCodeDto
													.getAttr());
											sqSubCodeDto.setBanned(false);
											sqSubCodeDto.setCode(subCodeDto
													.getCode());
											sqSubCodeDto
													.setEffective_period(subCodeDto
															.getEffectivePeriod());
											sqSubCodeDto.setStock(500);

											sqSubCodeDtoList.add(sqSubCodeDto);
										});

						c2.setSub_code_list(sqSubCodeDtoList);
					}

					catalog_list.add(c2);
				});

		return catalog_list;
	}
}